package guilayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controllayer.CustomerCtrl;


public class CustomerLookup extends AbstractSetCustomerNumber {
	//private CustomerCtrl customerCtrl;
	private JTextField txtCustomerNo;
	private JTextField txtFirstname;
	private JTextField txtLastname;
	private JTextField txtStreet;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private JTextField txtCity;
	private JTextField txtZipcode;
	private JLabel lblCustomerNo;
	private JLabel lblFirstname;
	private JLabel lblLastname;
	private JLabel lblStreet;
	private JLabel lblZipcode;
	private JLabel lblCity;
	private JLabel lblPhone;
	private JLabel lblEmail;
	private JPanel panel;
	private JButton btnEditCustomer;
	private JButton btnDeleteCustomer;

	/**
	 * Create the panel.
	 */
	public CustomerLookup() {
		super();
	}
	public CustomerLookup(String customerNo) {
		super(customerNo);
	}

	@Override
	protected JPanel loadThePage() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel pnlForm = new JPanel();
		// Remove the background
		pnlForm.setOpaque(false);
		add(pnlForm, BorderLayout.CENTER);
		pnlForm.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC}));
		
		lblCustomerNo = new JLabel("Kundenr.:");
		pnlForm.add(lblCustomerNo, "2, 2, left, default");
		
		txtCustomerNo = new JTextField();
		txtCustomerNo.setEditable(false);
		txtCustomerNo.setEnabled(false);
		txtCustomerNo.setText(customer.getCustomerNo());
		pnlForm.add(txtCustomerNo, "4, 2, fill, default");
		txtCustomerNo.setColumns(10);
		
		lblFirstname = new JLabel("Fornavn:");
		pnlForm.add(lblFirstname, "2, 4, left, default");
		
		txtFirstname = new JTextField();
		txtFirstname.setEditable(false);
		txtFirstname.setEnabled(false);
		txtFirstname.setText(customer.getFirstName());
		pnlForm.add(txtFirstname, "4, 4, fill, default");
		txtFirstname.setColumns(10);
		
		lblLastname = new JLabel("Efternavn:");
		pnlForm.add(lblLastname, "2, 6, left, default");
		
		txtLastname = new JTextField();
		txtLastname.setText(customer.getLastName());
		txtLastname.setEditable(false);
		txtLastname.setEnabled(false);
		pnlForm.add(txtLastname, "4, 6, fill, default");
		txtLastname.setColumns(10);
		
		lblStreet = new JLabel("Gadenavn + husnr.:");
		pnlForm.add(lblStreet, "2, 8, left, default");
		
		txtStreet = new JTextField();
		txtStreet.setText(customer.getAddressStreet());
		txtStreet.setEnabled(false);
		txtStreet.setEditable(false);
		pnlForm.add(txtStreet, "4, 8, fill, default");
		txtStreet.setColumns(10);
		
		lblZipcode = new JLabel("Postnummer:");
		pnlForm.add(lblZipcode, "2, 10, left, default");
		
		txtZipcode = new JTextField();
		txtZipcode.setText(customer.getAddressZipcode());
		txtZipcode.setEnabled(false);
		txtZipcode.setEditable(false);
		pnlForm.add(txtZipcode, "4, 10, fill, default");
		txtZipcode.setColumns(10);
		
		lblCity = new JLabel("By:");
		pnlForm.add(lblCity, "2, 12, left, default");
		
		txtCity = new JTextField();
		txtCity.setText(customer.getAddressCity());
		txtCity.setEditable(false);
		txtCity.setEnabled(false);
		pnlForm.add(txtCity, "4, 12, fill, default");
		txtCity.setColumns(10);
		
		lblPhone = new JLabel("Telefonnummer:");
		pnlForm.add(lblPhone, "2, 14, left, default");

		
		txtPhone = new JTextField();
		txtPhone.setText(customer.getPhone());
		txtPhone.setEditable(false);
		txtPhone.setEnabled(false);
		pnlForm.add(txtPhone, "4, 14, fill, default");
		txtPhone.setColumns(10);
		
		lblEmail = new JLabel("E-Mail:");
		pnlForm.add(lblEmail, "2, 16, left, default");
		
		txtEmail = new JTextField();
		txtEmail.setText(customer.getEmail());
		txtEmail.setEditable(false);
		txtEmail.setEnabled(false);
		pnlForm.add(txtEmail, "4, 16, fill, default");
		txtEmail.setColumns(10);
		
		panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);
		
		btnEditCustomer = new JButton("Rediger kunde");
		btnEditCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(1, 13, txtCustomerNo.getText());
			}
		});
		panel.add(btnEditCustomer);
		
		btnDeleteCustomer = new JButton("Slet kunde");
		btnDeleteCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(1, 14, txtCustomerNo.getText());
			}
		});
		panel.add(btnDeleteCustomer);

		return pnlForm;
	}

}
